package lightswitch.server.code.sqlhelper.expression;

import java.util.List;
import java.util.Vector;

import lightswitch.code.connection.QueryDataTransfering;
import lightswitch.server.code.object.DataRow;

import lightswitch.server.code.object.MyEntry;
import lightswitch.server.code.object.Property;
import lightswitch.server.code.object.Reference;


public abstract class AbstractTable {
	 public	String name="";
	 public String alias="";
	 
	 public List<Property> propertyList;
	 public List<DataRow> dataRowList;
	  public List<Reference> referencesList; 
		
		public void InitTable()
		{
//		    Table tb = Database.getLoadedTable(name);
//		    this.propertyList = tb.getListOfProperties();
//		    this.referencesList = tb.getListOfReferences();
//		    this.dataRowList = Table.convertDataRows(tb.getListOfDataRows(), this.alias);
		}
		
		// convert 3 list to its
		protected void InitTableWithAllLists(AbstractTable _tb) {
			for(int i=0;i<_tb.dataRowList.size();i++)
			{
				DataRow _row = new DataRow();
				for(int j=0;j<_tb.dataRowList.get(i).getSize();j++)
				{
					MyEntry en = _tb.dataRowList.get(i).getEntry(j);
					String [] str = en.getName().split("\\.");
					if(str.length!=2)
					{
						//throw exception
						//never occur
						return;
					}
					else
					{
						en.setName(this.alias+"."+str[1]);
					}
					_row.addEntry(en);
					
				}
				this.dataRowList.add(_row);
			}
			// gan property:
			
			for(int i=0;i<_tb.propertyList.size();i++)
			{
				Property pro = _tb.propertyList.get(i);
				String [] str = pro.getName().split("\\.");
				if(str.length!=2)
				{
					//throw exception
					//never occur
					return;
				}
				else
				{
					pro.setName(this.alias+"."+str[1]);
				}
				propertyList.add(pro);
				
				
			}
			
			// gan reference:
			for(int i=0;i<_tb.referencesList.size();i++)
			{
				Reference _ref = _tb.referencesList.get(i);
				referencesList.add(_ref);
			}
		}
		public NormalTable getAsNormalTable()
		{
			NormalTable _tb = new NormalTable();
			_tb.alias= alias;
			_tb.name = name;
			_tb.referencesList  = referencesList;
			
			
			
			
			//_tb.dataRowList = dataRowList;
			for(int i=0;i<dataRowList.size();i++)
			{
				_tb.dataRowList.add(new DataRow(dataRowList.get(i)));
			}			
			for(int i=0;i<propertyList.size();i++)
			{
				_tb.propertyList.add(new Property(propertyList.get(i)));
			}
			
			return _tb;
		}
		
		public static QueryDataTransfering convertToQueryDataTransfering(AbstractTable table){
	        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
	        List<DataRow> drs = table.dataRowList;
	        
	        for (DataRow dr : drs){
	            data.add(dr.getChainOfValues());
	        }
	        
	        return new QueryDataTransfering(data, table.toColumnTransfer(), false);
	    }
		
		public Vector<String> toColumnTransfer(){
	        Vector<String> result = new Vector<String>();
	        for (Property p : propertyList){
	            result.add(p.alias);
	        }
	        return result;
	    }
		
	}
